/*     */ package com.zimbra.cs.mailclient.pop3;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.cs.mailclient.CommandFailedException;
/*     */ import com.zimbra.cs.mailclient.MailConnection;
/*     */ import com.zimbra.cs.mailclient.MailConnection.State;
/*     */ import com.zimbra.cs.mailclient.MailException;
/*     */ import com.zimbra.cs.mailclient.MailInputStream;
/*     */ import com.zimbra.cs.mailclient.MailOutputStream;
/*     */ import com.zimbra.cs.mailclient.auth.Authenticator;
/*     */ import com.zimbra.cs.mailclient.util.Ascii;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.OutputStream;
/*     */ import javax.security.auth.login.LoginException;
/*     */ import org.apache.commons.codec.binary.Base64;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class Pop3Connection
/*     */   extends MailConnection
/*     */ {
/*     */   private Pop3Capabilities capabilities;
/*     */   private int messageCount;
/*     */   private long maildropSize;
/*     */   private static final String PASS = "PASS";
/*     */   private static final String USER = "USER";
/*     */   private static final String AUTH = "AUTH";
/*     */   private static final String STLS = "STLS";
/*     */   private static final String QUIT = "QUIT";
/*     */   private static final String CAPA = "CAPA";
/*     */   private static final String STAT = "STAT";
/*     */   private static final String LIST = "LIST";
/*     */   private static final String UIDL = "UIDL";
/*     */   private static final String DELE = "DELE";
/*     */   private static final String NOOP = "NOOP";
/*     */   private static final String RSET = "RSET";
/*     */   private static final String RETR = "RETR";
/*     */   
/*     */   public Pop3Connection(Pop3Config config)
/*     */   {
/*  54 */     super(config);
/*     */   }
/*     */   
/*     */   protected MailInputStream newMailInputStream(InputStream is)
/*     */   {
/*  59 */     if (getLogger().isTraceEnabled()) {
/*  60 */       return new MailInputStream(is, getLogger());
/*     */     }
/*  62 */     return new MailInputStream(is);
/*     */   }
/*     */   
/*     */ 
/*     */   protected MailOutputStream newMailOutputStream(OutputStream os)
/*     */   {
/*  68 */     if (getLogger().isTraceEnabled()) {
/*  69 */       return new MailOutputStream(os, getLogger());
/*     */     }
/*  71 */     return new MailOutputStream(os);
/*     */   }
/*     */   
/*     */   protected void processGreeting()
/*     */     throws IOException
/*     */   {
/*  77 */     Pop3Response res = Pop3Response.read(null, this.mailIn);
/*  78 */     this.greeting = res.getMessage();
/*  79 */     if (!res.isOK()) {
/*  80 */       throw new MailException("Expected greeting, but got: " + res.getMessage());
/*     */     }
/*     */     
/*  83 */     setState(MailConnection.State.NOT_AUTHENTICATED);
/*  84 */     this.capabilities = capa();
/*     */   }
/*     */   
/*     */   public void login(String pass) throws IOException
/*     */   {
/*  89 */     super.login(pass);
/*  90 */     this.capabilities = capa();
/*  91 */     stat();
/*     */   }
/*     */   
/*     */   public void authenticate(String pass) throws LoginException, IOException
/*     */   {
/*  96 */     super.authenticate(pass);
/*  97 */     this.capabilities = capa();
/*  98 */     stat();
/*     */   }
/*     */   
/*     */   public void logout() throws IOException
/*     */   {
/* 103 */     quit();
/*     */   }
/*     */   
/*     */   protected void sendLogin(String user, String pass) throws IOException
/*     */   {
/* 108 */     sendCommandCheckStatus("USER", user);
/* 109 */     sendCommandCheckStatus("PASS", pass);
/*     */   }
/*     */   
/*     */   protected void sendAuthenticate(boolean ir) throws IOException
/*     */   {
/* 114 */     StringBuffer sb = new StringBuffer(this.authenticator.getMechanism());
/* 115 */     if (ir) {
/* 116 */       byte[] response = this.authenticator.getInitialResponse();
/* 117 */       sb.append(' ');
/* 118 */       sb.append(Ascii.toString(Base64.encodeBase64(response)));
/*     */     }
/* 120 */     sendCommandCheckStatus("AUTH", sb.toString());
/*     */   }
/*     */   
/*     */   protected void sendStartTls() throws IOException
/*     */   {
/* 125 */     sendCommandCheckStatus("STLS", null);
/*     */   }
/*     */   
/*     */   private Pop3Capabilities capa() throws IOException {
/* 129 */     Pop3Response res = sendCommand("CAPA", null);
/* 130 */     if (!res.isOK()) {
/* 131 */       return null;
/*     */     }
/*     */     try {
/* 134 */       return Pop3Capabilities.read(res.getContentInputStream());
/*     */     } finally {
/* 136 */       res.dispose();
/*     */     }
/*     */   }
/*     */   
/*     */   private void stat() throws IOException {
/* 141 */     Pop3Response res = sendCommandCheckStatus("STAT", null);
/* 142 */     String[] parts = split(res.getMessage());
/* 143 */     if (parts.length > 1) {
/*     */       try {
/* 145 */         this.messageCount = Integer.parseInt(parts[0]);
/* 146 */         this.maildropSize = Long.parseLong(parts[1]);
/* 147 */         return;
/*     */       } catch (NumberFormatException e) {
/* 149 */         getLogger().error("Invalid STAT response: " + res.getMessage(), e);
/*     */       }
/*     */     }
/*     */     
/* 153 */     throw new CommandFailedException("STAT", "Invalid STAT response: " + res.getMessage());
/*     */   }
/*     */   
/*     */   public Pop3Capabilities getCapabilities()
/*     */   {
/* 158 */     return this.capabilities;
/*     */   }
/*     */   
/*     */   public int getMessageCount() {
/* 162 */     return this.messageCount;
/*     */   }
/*     */   
/*     */   public long getMaildropSize() {
/* 166 */     return this.maildropSize;
/*     */   }
/*     */   
/*     */   public int getMessageSize(int msgno) throws IOException {
/* 170 */     Pop3Response res = sendCommand("LIST", Integer.valueOf(msgno));
/* 171 */     if (res.isOK()) {
/* 172 */       String[] parts = split(res.getMessage());
/* 173 */       if (parts.length > 1) {
/*     */         try {
/* 175 */           return Integer.parseInt(parts[1]);
/*     */         }
/*     */         catch (NumberFormatException e) {}
/*     */       }
/*     */       
/* 180 */       throw new CommandFailedException("LIST", "Invalid LIST response: " + res.getMessage());
/*     */     }
/*     */     
/* 183 */     return -1;
/*     */   }
/*     */   
/*     */   public Integer[] getMessageSizes() throws IOException {
/* 187 */     Integer[] sizes = new Integer[this.messageCount];
/* 188 */     Pop3Response res = sendCommandCheckStatus("LIST", null);
/*     */     try {
/* 190 */       ContentInputStream is = res.getContentInputStream();
/*     */       String line;
/* 192 */       String[] parts; while ((line = is.readLine()) != null) {
/* 193 */         parts = split(line);
/* 194 */         if (parts.length < 2) {
/*     */           break;
/*     */         }
/*     */         try {
/* 198 */           int msgno = Integer.parseInt(parts[0]);
/* 199 */           int size = Integer.parseInt(parts[1]);
/* 200 */           sizes[(msgno - 1)] = Integer.valueOf(size);
/*     */         } catch (Exception e) {
/*     */           break;
/*     */         }
/*     */       }
/* 205 */       if (line != null) {
/* 206 */         throw new CommandFailedException("LIST", "Invalid LIST response: " + line);
/*     */       }
/*     */       
/* 209 */       return sizes;
/*     */     } finally {
/* 211 */       res.dispose();
/*     */     }
/*     */   }
/*     */   
/*     */   public String getMessageUid(int msgno) throws IOException {
/* 216 */     Pop3Response res = sendCommand("UIDL", Integer.valueOf(msgno));
/* 217 */     if (res.isOK()) {
/* 218 */       String[] parts = split(res.getMessage());
/* 219 */       if (parts.length < 2) {
/* 220 */         throw new CommandFailedException("UIDL", "Invalid UIDL response: " + res.getMessage());
/*     */       }
/*     */       
/* 223 */       return parts[1];
/*     */     }
/* 225 */     return null;
/*     */   }
/*     */   
/*     */   public String[] getMessageUids() throws IOException {
/* 229 */     String[] uids = new String[this.messageCount];
/* 230 */     Pop3Response res = sendCommandCheckStatus("UIDL", null);
/*     */     try {
/* 232 */       ContentInputStream is = res.getContentInputStream();
/*     */       String line;
/* 234 */       String[] parts; while ((line = is.readLine()) != null) {
/* 235 */         parts = split(line);
/* 236 */         if (parts.length < 2) {
/*     */           break;
/*     */         }
/*     */         try {
/* 240 */           int msgno = Integer.parseInt(parts[0]);
/* 241 */           String uid = parts[1];
/* 242 */           uids[(msgno - 1)] = uid;
/*     */         } catch (Exception e) {
/*     */           break;
/*     */         }
/*     */       }
/* 247 */       if (line != null) {
/* 248 */         throw new CommandFailedException("LIST", "Invalid UIDL response: " + line);
/*     */       }
/*     */       
/* 251 */       return uids;
/*     */     } finally {
/* 253 */       res.dispose();
/*     */     }
/*     */   }
/*     */   
/*     */   public ContentInputStream getMessage(int msgno) throws IOException {
/* 258 */     return sendCommandCheckStatus("RETR", Integer.valueOf(msgno)).getContentInputStream();
/*     */   }
/*     */   
/*     */   public boolean deleteMessage(int msgno) throws IOException {
/* 262 */     return sendCommand("DELE", Integer.valueOf(msgno)).isOK();
/*     */   }
/*     */   
/*     */   public void reset() throws IOException {
/* 266 */     sendCommandCheckStatus("RSET", null);
/*     */   }
/*     */   
/*     */   public void noop() throws IOException {
/* 270 */     sendCommandCheckStatus("NOOP", null);
/*     */   }
/*     */   
/*     */   public void quit() throws IOException {
/* 274 */     setState(MailConnection.State.LOGOUT);
/* 275 */     sendCommandCheckStatus("QUIT", null);
/* 276 */     setState(MailConnection.State.CLOSED);
/*     */   }
/*     */   
/*     */   public boolean hasCapability(String cap) {
/* 280 */     return (this.capabilities != null) && (this.capabilities.hasCapability(cap));
/*     */   }
/*     */   
/*     */   public Pop3Config getPop3Config() {
/* 284 */     return (Pop3Config)this.config;
/*     */   }
/*     */   
/*     */   public Pop3Response sendCommand(String cmd, Object args) throws IOException {
/* 288 */     this.mailOut.write(cmd);
/* 289 */     if (args != null) {
/* 290 */       this.mailOut.write(32);
/* 291 */       if (cmd.equalsIgnoreCase("PASS")) {
/* 292 */         writePass(args.toString());
/*     */       } else {
/* 294 */         this.mailOut.write(args.toString());
/*     */       }
/*     */     }
/* 297 */     this.mailOut.newLine();
/* 298 */     this.mailOut.flush();
/* 299 */     this.mailOut.trace();
/*     */     for (;;) {
/* 301 */       Pop3Response res = Pop3Response.read(cmd, this.mailIn);
/* 302 */       if (!res.isContinuation()) {
/* 303 */         return res;
/*     */       }
/* 305 */       processContinuation(res.getMessage());
/*     */     }
/*     */   }
/*     */   
/*     */   private void writePass(String pass) throws IOException {
/* 310 */     this.mailOut.setPrivacy(true);
/* 311 */     this.mailOut.write(pass);
/* 312 */     this.mailOut.setPrivacy(false);
/*     */   }
/*     */   
/*     */   public Pop3Response sendCommandCheckStatus(String cmd, Object args) throws IOException
/*     */   {
/* 317 */     Pop3Response res = sendCommand(cmd, args);
/* 318 */     if (!res.isOK()) {
/* 319 */       throw new CommandFailedException(cmd, res.getMessage());
/*     */     }
/* 321 */     return res;
/*     */   }
/*     */   
/*     */   private static String[] split(String s) {
/* 325 */     return s.trim().split("[ \\t]+");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailclient/pop3/Pop3Connection.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */